package com.dou.you.che.service.impl.sys;

import com.dou.you.che.service.sys.SysSmsLogServiceI;
import com.dou.you.che.comm.utils.IdUtil;
import com.dou.you.che.comm.utils.constant.ConfigConstant;
import com.dou.you.che.comm.utils.date.DateUtils;
import com.dou.you.che.comm.utils.sms.SmsUtil;
import com.dou.you.che.core.entity.conf.SmsConfig;
import com.dou.you.che.core.entity.sys.SysSmsLog;
import com.dou.you.che.core.mapper.sys.SysSmsLogDao;
import com.dou.you.che.service.sys.SysConfigServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 短消息
 * @Author 黄国刚【1058118328@qq.com】
 */
@Service
@Transactional(readOnly = true)
public class SysSmsLogService implements SysSmsLogServiceI {

    @Autowired
    private SysSmsLogDao smsLogDao;
    @Autowired
    private SysConfigServiceI sysConfigService;

    @Override
    public SysSmsLog queryObject(String id) {
        return smsLogDao.queryObject(id);
    }

    @Override
    public List<SysSmsLog> queryList(Map<String, Object> map) {
        return smsLogDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return smsLogDao.queryTotal(map);
    }

    @Transactional(readOnly = false)
    @Override
    public int save(SysSmsLog smsLog) {
        smsLog.setId(IdUtil.createIdbyUUID());
        return smsLogDao.save(smsLog);
    }

    @Transactional(readOnly = false)
    @Override
    public int update(SysSmsLog smsLog) {
        return smsLogDao.update(smsLog);
    }

    @Transactional(readOnly = false)
    @Override
    public int delete(String id) {
        return smsLogDao.delete(id);
    }

    @Transactional(readOnly = false)
    @Override
    public int deleteBatch(String[] ids) {
        return smsLogDao.deleteBatch(ids);
    }

    @Override
    public SysSmsLog sendSms(SysSmsLog smsLog) {
        String result = "";
        //获取云存储配置信息
        SmsConfig config = sysConfigService.getConfigObject(ConfigConstant.SMS_CONFIG_KEY, SmsConfig.class);
        try {
            /**
             * 状态,发送编号,无效号码数,成功提交数,黑名单数和消息，无论发送的号码是多少，一个发送请求只返回一个sendid，如果响应的状态不是“0”，则只有状态和消息
             */
            result = SmsUtil.crSendSms(config.getName(), config.getPwd(), smsLog.getMobile(), smsLog.getContent(), config.getSign(),
                    DateUtils.formTimeString(smsLog.getStime(), "yyyy-MM-dd HH:mm:ss"), smsLog.getExtno());
        } catch (Exception e) {

        }
        String arr[] = result.split(",");
        //发送成功
        if ("0".equals(arr[0])) {
            smsLog.setSendId(arr[1]);
            smsLog.setInvalidNum(Integer.parseInt(arr[2]));
            smsLog.setSuccessNum(Integer.parseInt(arr[3]));
            smsLog.setBlackNum(Integer.parseInt(arr[4]));
            smsLog.setReturnMsg(arr[5]);
        } else {
            //发送失败
            smsLog.setReturnMsg(arr[1]);
        }
        smsLog.setSendStatus(Integer.parseInt(arr[0]));
        smsLog.setSign(config.getSign());
        if (null == smsLog.getStime()) {
            smsLog.setStime(new Date());
        }
        //保存发送记录
        save(smsLog);
        return smsLog;
    }
}
